package com.viewshine.nettydemo.springBeanMethod.nettyClient;

import com.viewshine.nettydemo.springBeanMethod.nettyVo.NettyRequest;
import com.viewshine.nettydemo.springBeanMethod.nettyVo.NettyRespose;
import com.viewshine.nettydemo.staticMethod.nettyClient.BasicsEchoClientHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Properties;

/**
 * @ClassName BasicsEchoClient
 * @Author rhb
 * @Description SpringBean管理 客户端
 * @Date 2019-01-17 15:57
 * @Version 1.0
 **/
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) //多列管理
@Slf4j
public class SpringEchoClient {

    /**
     * 资源文件配置ip地址和端口号
     */
    private static Properties properties;

    private Integer port;

    private String host;

    /**
     * 构造器
     */
    public SpringEchoClient() {
        try {
            if (null == properties)
                properties = PropertiesLoaderUtils.loadAllProperties("config.properties");
            /*设置默认值127.0.0.1：12007*/
            String ip = properties.getProperty("my.netty.client.host", "127.0.0.1");
            String port = properties.getProperty("my.netty.client.port", "12007");
            this.host = ip;
            this.port = Integer.valueOf(port);
            log.debug("连接【{}:{}】", ip, port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 标准处理对象
     */
    private SpringEchoClientHandler echoClientHandler;

    /**
     * 返回结果对象
     */
    private NettyRespose nettyRespose = new NettyRespose();

    /**
     * 获取处理器。
     *
     * @return
     */
    private ChannelHandler getChannel() {
        return echoClientHandler;
    }


    /**
     * 获取返回结果。
     *
     * @return
     */
    public NettyRespose getNettyRespose() {
        return echoClientHandler.getNettyRespose();
    }

    /**
     * 设置发送到模型
     *
     * @param nettyRequest
     */
    public void setNettyRequest(NettyRequest nettyRequest) {
        echoClientHandler = new SpringEchoClientHandler(nettyRequest);
    }


    /**
     * 发送
     *
     * @throws Exception
     */
    public void send() throws Exception {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group);
            b.channel(NioSocketChannel.class);
            b.remoteAddress(new InetSocketAddress(host, port));
            b.handler(new ChannelInitializer<SocketChannel>() {
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(getChannel());
                }
            });
            ChannelFuture f = b.connect().sync();

            f.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully().sync();
        }
    }



}
